package com.mxchip.livestarmobile.mobile.ui.recent;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.mxchip.livestarmobile.R;
import com.mxchip.livestarmobile.mobile.db.entity.CallLog;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


public class RecentCallDialog extends Dialog {


    private static final String TAG = "RecentCallDialog";

    private OnClickListener onClickListener;

    private CallLog bean;

    public void setBean(CallLog bean) {
        this.bean = bean;
    }


    public void onButtonClick(View view) {
        Log.e(TAG, "onCallButtonClick");

        if (onClickListener != null) {
            onClickListener.onClick(this, Integer.parseInt((String) view.getTag()));
        }
    }

    public void setOnClickListener(OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public RecentCallDialog(@NonNull Context context) {
        super(context);
    }

    public RecentCallDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected RecentCallDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        hideSystemUI();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_recent_call);

        TextView titleTextView = findViewById(R.id.title_text_view);

        findViewById(R.id.call_button).setOnClickListener(this::onButtonClick);
        findViewById(R.id.add_button).setOnClickListener(this::onButtonClick);
        findViewById(R.id.delete_button).setOnClickListener(this::onButtonClick);

        if (bean != null) {

            String title = "";

          /*  if (bean.getName() != null) {
                title = bean.getName();
            } else*/
            if (bean.getNumber() != null) {
                title = bean.getNumber();
            }

            titleTextView.setText(title);

//            if (bean.getName() != null) {
//                findViewById(R.id.add_button).setVisibility(View.GONE);
//            }
        }
    }

    @Override
    public void show() {
        // Set the dialog to not focusable.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);

        // Show the dialog with NavBar hidden.
        super.show();

        // Set the dialog to focusable again.
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
    }


    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }

    private void hideSystemUI() {
        // Enables regular immersive mode.
        // For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
        // Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE
                        // Set the content to appear under the system bars so that the
                        // content doesn't resize when the system bars hide and show.
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        // Hide the nav bar and status bar
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
    }

    // Shows the system bars by removing all the flags
// except for the ones that make the content appear under the system bars.
    private void showSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
    }
}
